import { useRequest } from "../../../utils/request";
import { getUserInfo } from "../../../service/user";

const WeValidator = require('../../../package/we-validator.min')

Component({
    properties: {},
    data: {},
    methods: {
        onLoad() {
            this.validatorInstance = new WeValidator({
                rules: {
                    nickname: {
                        required: true,
                    },
                },
                messages: {
                    nickname: {
                        required: '请输入姓名',
                    },
                },
            })

            this.request = new useRequest('api/edituserinfo', { hold: true }, this)
        },

        submit(e) {
            const value = e.detail.value

            if (this.validatorInstance.checkData(value)) {
                this.request
                    .run(value)
                    .then(() => {
                        wx.showToast({
                            title: '更换成功'
                        })
                        getUserInfo().then()
                        wx.navigateBack()
                    })
            }
        }
    }
});
